////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef __WN_CONCURRENCY_SPIN_LOCK_H__
#define __WN_CONCURRENCY_SPIN_LOCK_H__

#include "WNCore/inc/WNPlatform.h"
#include "WNCore/inc/WNTypes.h"
#include "WNCore/inc/WNMacros.h"

namespace WNConcurrency {
    class WNSpinLock {
    public:
        WN_INLINE WNSpinLock();

        WN_INLINE WN_VOID Lock();
        WN_INLINE WN_BOOL TryLock();
        WN_INLINE WN_VOID Unlock();

    private:
        WN_ATOM_T mCounter;
    };

    class WNSpinLockScopeLock {
    public:
        WN_INLINE WNSpinLockScopeLock(WNSpinLock& _spinLock);
        WN_INLINE ~WNSpinLockScopeLock();

    private:
        WNSpinLock& mSpinLock;

        WN_BOOL mLocked;
    };
}

#define WN_SPIN_LOCK_SCOPE_LOCK(_spinLock) WNSpinLockScopeLock WN_UNIQUE_VARIABLE(spinLock)(_spinLock)

#include "WNConcurrency/inc/WNSpinLock.inl"

#endif // __WN_CONCURRENCY_SPIN_LOCK_H__